package com.docshell.common;

import com.docshell.exceptions.DocShellException;
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;

public class DbManager {
	
	private static IDfSession dfSession = null;
	
	public static IDfCollection getData(String sqlStr) throws DfException, DocShellException {
		IDfClientX loClientx = new DfClientX();
		IDfCollection loColl = null;
		if (dfSession==null) dfSession = SessionManager.getSession();
		try {
			
			IDfQuery qry = loClientx.getQuery();
			qry.setDQL(sqlStr);
			loColl = qry.execute(dfSession,DfQuery.READ_QUERY);
		
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.getMessage());
		}
		return loColl;	
	}
	
	public static boolean execQuery(String sqlStr) throws DfException, DocShellException
	{
		IDfClientX loClientx = new DfClientX();
		boolean lbResult = false;
		try {
			if (dfSession==null) dfSession = SessionManager.getSession();
			IDfQuery qry = loClientx.getQuery();
			qry.setDQL(sqlStr);
			qry.execute(dfSession,DfQuery.EXEC_QUERY);
			lbResult = true;
			
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.getMessage());
		}
		return lbResult;	
	}

	public static String getUserHomeCabinet() throws DfException, DocShellException {
		String lsHomeCabinet = "/Temp";
		try {
			String lsQry = "SELECT default_folder FROM dm_user where user_os_name = '" + SessionManager.getUserName() + "'";
			IDfCollection dfColl = getData(lsQry);
			if (dfColl!=null) {
				dfColl.next();
				lsHomeCabinet = dfColl.getString("default_folder");
			}
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.getMessage());
		}
		return lsHomeCabinet;
	}

	public static boolean checkFolderPath(String startupFolder) throws DfException, DocShellException {
		boolean result = false;
		try {
			if (startupFolder.equals("/")) {
				result=true;
			}
			else {
				if (dfSession==null) dfSession = SessionManager.getSession();
				IDfFolder folder = dfSession.getFolderByPath(startupFolder);
				if (folder!=null) result = true;
			}
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.getMessage());
		}
		return result;		
	}

	public static boolean checkFolderPermission(String lsTargerFolder, int aiPermision) throws DfException, DocShellException {
		boolean result = false;
		try {
			if (dfSession==null) dfSession = SessionManager.getSession();
			IDfFolder folder = dfSession.getFolderByPath(lsTargerFolder);
			if (folder!=null) {
				result = folder.getPermitEx(null)>=aiPermision ? true : false;
			}
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.getMessage());
		}
		return result;	
	}

	public static boolean createFolder(String lsTargetFolder, String lsTargetPath) throws DfException, DocShellException, DfException {
		
		String lsSQL = "CREATE dm_folder OBJECT set object_name = '" + lsTargetFolder + "' LINK '" + lsTargetPath + "'";
		return execQuery(lsSQL);
		
	}

	public static boolean removeObject(String lsTargetObject, String lsTargetPath) throws DocShellException, DfException {
		String lsSQL = "DELETE dm_sysobject OBJECT WHERE object_name = '" + lsTargetObject + "' AND FOLDER('" + lsTargetPath + "')";
		return execQuery(lsSQL);
	}
}
